package com.xhu.collegestudentrentalsystem.interceptor;

import cn.hutool.json.JSONUtil;
import com.xhu.collegestudentrentalsystem.module.user.entity.House;
import com.xhu.collegestudentrentalsystem.module.user.entity.UserMark;
import com.xhu.collegestudentrentalsystem.module.user.service.IHouseService;
import com.xhu.collegestudentrentalsystem.module.user.service.IMarkService;
import com.xhu.collegestudentrentalsystem.module.user.service.impl.MarkServiceImpl;
import com.xhu.collegestudentrentalsystem.util.CacheUtils;
import com.xhu.collegestudentrentalsystem.util.HostHolder;
import com.xhu.collegestudentrentalsystem.util.constant.KeyMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/23 13:22
 *  在请求访问Index页面时，拦截处理获取房子信息功能
 */
@Component
public class IndexInterceptor implements HandlerInterceptor {

    private static final Logger logger= LoggerFactory.getLogger(LoginInterceptor.class);

    @Autowired
    private IHouseService houseService;

    @Autowired
    private CacheUtils cacheUtils;

    @Autowired
    private IMarkService markService;

    @Autowired
    private HostHolder hostHolder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断是否是请求首页页面s
        String uri = request.getRequestURI();
        if("/xhu_rent/index".equals(uri)){
            List<House> houses = new ArrayList<>();
            // 查询最新推荐
            List<House> housesNew = houseService.findHouseNew();
            houses.addAll(housesNew);
            // 查询高分优选
            List<House> housesHighScore = houseService.findHouseHighScore();
            houses.addAll(housesHighScore);
            // 查询房子被收藏情况
            for(House house: houses) {
                if (hostHolder.getUser() != null) {
                    UserMark mark = markService.findUserMarkById(house.getId(), hostHolder.getUser().getId());
                    if(mark != null){
                        house.setIsMarked(true);
                    }else {
                        house.setIsMarked(false);
                    }
                }else {
                    house.setIsMarked(false);
                }
            }
            cacheUtils.getLocalCache().put(KeyMsg.house.HOUSE_LIST_NEWS, JSONUtil.toJsonStr(housesNew));
            cacheUtils.getLocalCache().put(KeyMsg.house.HOUSE_LIST_HIGH_SCORE, JSONUtil.toJsonStr(housesHighScore));
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String housesNew = cacheUtils.getLocalCache().getIfPresent(KeyMsg.house.HOUSE_LIST_NEWS);
        String housesHighScore = cacheUtils.getLocalCache().getIfPresent(KeyMsg.house.HOUSE_LIST_HIGH_SCORE);
        // 将格式转为对象格式
        List<House> houses1 = JSONUtil.toList(housesNew, House.class);
        List<House> houses2 = JSONUtil.toList(housesHighScore, House.class);
        modelAndView.setViewName("index");
        modelAndView.addObject(KeyMsg.house.HOUSE_LIST_NEWS, houses1);
        modelAndView.addObject(KeyMsg.house.HOUSE_LIST_HIGH_SCORE, houses2);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
